package com.c2b.algorithm.leetcode.jzoffer;

/**
 * <a href="https://leetcode.cn/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/">左旋转字符串</a>
 * <p>字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。<br>
 * 比如，输入字符串"abcdefg"和数字2，该函数将返回左旋转两位得到的结果"cdefgab"。</p>
 *
 * <pre>
 * 示例 1：
 *      输入: s = "abcdefg", k = 2
 *      输出: "cdefgab"
 *
 * 示例 2：
 *      输入: s = "lrloseumgh", k = 6
 *      输出: "umghlrlose"
 * </pre>
 *
 * @author c2b
 * @since 2023/4/7 9:20
 */
public class JzOffer0058ReverseLeftWords_II {
    /**
     * 字符串切片
     * <p>复杂度分析：</p>
     * 时间复杂度 O(N)： 其中 N 为字符串 s 的长度，字符串切片函数为线性时间复杂度（参考资料）；<br>
     * 空间复杂度 O(N)： 两个字符串切片的总长度为 N 。<br>
     */
    public String reverseLeftWords(String s, int n) {
        if (s == null) {
            return null;
        }
        return s.substring(n) + s.substring(0, n);
    }

    public String reverseLeftWords2(String s, int n) {
        if (s == null) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = n; i < s.length(); i++) {
            stringBuilder.append(s.charAt(i));
        }
        for (int i = 0; i < n; i++) {
            stringBuilder.append(s.charAt(i));
        }
        return stringBuilder.toString();
    }

    public String reverseLeftWords2Plus(String s, int n) {
        if (s == null) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = n; i < n + s.length(); i++) {
            stringBuilder.append(s.charAt(i % s.length()));
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        JzOffer0058ReverseLeftWords_II jzOffer0058ReverseLeftWords_ii = new JzOffer0058ReverseLeftWords_II();
        String abcdefg = jzOffer0058ReverseLeftWords_ii.reverseLeftWords("abcdefg", 2);
        System.out.println(abcdefg);
    }
}
